// Copyright 2022 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.mediapipe.tasks.vision.handlandmarker;

import com.google.auto.value.AutoValue;
import com.google.mediapipe.formats.proto.LandmarkProto;
import com.google.mediapipe.formats.proto.ClassificationProto.ClassificationList;
import com.google.mediapipe.tasks.components.containers.Category;
import com.google.mediapipe.tasks.components.containers.Landmark;
import com.google.mediapipe.tasks.components.containers.NormalizedLandmark;
import com.google.mediapipe.tasks.core.TaskResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** Represents the hand landmarks deection results generated by {@link HandLandmarker}. */
@AutoValue
public abstract class HandLandmarkerResult implements TaskResult {

  /**
   * Creates a {@link HandLandmarkerResult} instance from the lists of landmarks and handedness
   * protobuf messages.
   *
   * @param landmarksProtos a List of {@link NormalizedLandmarkList}
   * @param worldLandmarksProtos a List of {@link LandmarkList}
   * @param handednessesProtos a List of {@link ClassificationList}
   */
  static HandLandmarkerResult create(
      List<LandmarkProto.NormalizedLandmarkList> landmarksProtos,
      List<LandmarkProto.LandmarkList> worldLandmarksProtos,
      List<ClassificationList> handednessesProtos,
      long timestampMs) {
    List<List<NormalizedLandmark>> handLandmarks = new ArrayList<>();
    for (LandmarkProto.NormalizedLandmarkList handLandmarksProto : landmarksProtos) {
      handLandmarks.add(
          Collections.unmodifiableList(NormalizedLandmark.createListFromProto(handLandmarksProto)));
    }

    List<List<Landmark>> handWorldLandmarks = new ArrayList<>();
    for (LandmarkProto.LandmarkList handWorldLandmarksProto : worldLandmarksProtos) {
      handWorldLandmarks.add(
          Collections.unmodifiableList(Landmark.createListFromProto(handWorldLandmarksProto)));
    }

    List<List<Category>> handHandednesses = new ArrayList<>();
    for (ClassificationList handednessProto : handednessesProtos) {
      handHandednesses.add(
          Collections.unmodifiableList(Category.createListFromProto(handednessProto)));
    }

    return new AutoValue_HandLandmarkerResult(
        timestampMs,
        Collections.unmodifiableList(handLandmarks),
        Collections.unmodifiableList(handWorldLandmarks),
        Collections.unmodifiableList(handHandednesses));
  }

  @Override
  public abstract long timestampMs();

  /** Hand landmarks of detected hands. */
  public abstract List<List<NormalizedLandmark>> landmarks();

  /** Hand landmarks in world coordinates of detected hands. */
  public abstract List<List<Landmark>> worldLandmarks();

  /**
   * Handedness of detected hands.
   *
   * @deprecated Use {@link #handedness()} instead.
   */
  @Deprecated
  public List<List<Category>> handednesses() {
    return handedness();
  }

  /** Handedness of detected hands. */
  public abstract List<List<Category>> handedness();
}
